import QtQuick
import QtQuick.Controls 2.7
import FlatUIControls 1.0

Item {
    id: root
    width: 400
    height: settings_btn.checked ? 200 : 40
    clip: true

    property bool simple: true

    // 向Console
    signal sigRequestDeviceList()
    signal sigUpdateSettings(baud: int, dlen: int, parity: int, stb: int)
    signal sigConnect(com: string)
    signal sigDisconnect()

    function refreshDeviceList(namelist){
        console.log(namelist)
        serial_name.model = namelist
    }

    function refreshComState(state){
        console.log(state)
        if(state) {
            connect_btn.checked = true;
        }else{
            connect_btn.checked = false;
        }
    }

    Row {
        spacing: 2
        Image {
            id: name
            width: 40
            height: 40
            source: "qrc:/flatUI/img/images/serialport.png"
        }
        // 串口号下拉框
        DropDown {
            id: serial_name
            width: 152
            height: 40
            model: ["COM3"]
        }
        // 刷新按键
        ImageButton {
            id: refresh_btn
            imgUrlPress: "qrc:/flatUI/img/images/refresh.png"
            imgUrlRelease: "qrc:/flatUI/img/images/refresh.png"
            onClicked: {
                sigRequestDeviceList();
            }
        }
        // 连接按键
        ImageButton {
            id: connect_btn
            imgUrlPress: "qrc:/flatUI/img/images/stop_r.png"
            imgUrlRelease: "qrc:/flatUI/img/images/start_r.png"
            onClicked: {
                if (!checked) {
                    sigUpdateSettings(settings_view.baudrate, settings_view.data_bits, settings_view.check_type, settings_view.stop_bits);
                    sigConnect(serial_name.currentValue);
                }
                else {
                    sigDisconnect()
                }
            }
        }
        // 设置菜单
        ImageButton {
            id: settings_btn
            enabled: false
            imgUrlPress: "qrc:/flatUI/img/images/settings_p.png"
            imgUrlRelease: "qrc:/flatUI/img/images/settings_r.png"
            checkable: true
        }
    }
    // 菜单列表
    SerialPortSettings {
        id: settings_view
        y: 42
    }
    // 高度变化动画效果
    Behavior on height { NumberAnimation { duration: 400 }}

    Component.onCompleted: {
        // 如果是简化版本的，隐藏多余设置菜单
        if(!simple) {settings_btn.enabled = true}
        // 同步设置成默认设置
        settings_view.display_init(5, 3, 0, 0, 0);
    }
}
